<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="领用单统计"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <produceUseTj ref="produceUseTj"></produceUseTj>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="detailOnSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>
    
    <script>
import VolBox from "@/components/basic/VolBox.vue";
import produceUseTj from "@/views/produceinfo/produce_use_tj/q_produce_use_tj.vue";
export default {
  components: {
    "vol-box": VolBox,
    produceUseTj,
  },
  data() {
    return {
      model: false,
      WHSE_Id: null,
    };
  },
  methods: {
    async onModelClose() {
      // console.log("关闭");
      this.$refs.produceUseTj.searchFormFields.领用单号 = null;
      this.$refs.produceUseTj.searchFormFields.领用人 = null;
      this.$refs.produceUseTj.searchFormFields.产品编码 = null;
      this.$refs.produceUseTj.searchFormFields.产品名称 = null;
      this.$refs.produceUseTj.searchFormFields.sczt = null;
      this.WHSE_Id = null;
      await this.$refs.produceUseTj.$refs.grid.search();
    },
    async open(Proposer, WHSE_Id) {
      this.model = true;
      this.WHSE_Id = WHSE_Id;
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.produceUseTj) {
          clearInterval(setIntervalTiem);
          this.$refs.produceUseTj.searchFormFields.机构 = WHSE_Id;
          // this.$refs.produceUseTj.searchFormFields.领用人 = Proposer;
          this.$refs.produceUseTj.searchFormFields.sczt = "1";
          this.$refs.produceUseTj.searchFormOptions.forEach(item => {
            item.forEach(x=>{
              if (x.field=="机构") {//||x.field=="领用人"
                x.disabled=true
              }
            })
          });
          this.$refs.produceUseTj.$refs.grid.search();
        }
      }, 100);
    },
    async detailOnSelect() {
      // let selectionRows = this.$refs.produceUseTj.$refs.grid.getSelectRows();
      let rowObj = this.$refs.produceUseTj.$refs.grid.getSelectRows();

      if (rowObj.length) {
        //  判断审核未通过数据，后期放开
        let shztSome = rowObj.some((item) => item.shzt != 1);
        if (shztSome) {
          if (this.messageError) {
            this.messageError.close();
          }
          this.messageError =
            this.$message.error("请勿选择审核未完成的领用调整单");
          return;
        }
        let scztSome = rowObj.some((item) => item.sczt != 1);
        if (scztSome) {
          if (this.messageError) {
            this.messageError.close();
          }
          this.messageError = this.$message.error("请选择生产中数据");
          return;
        }

        this.$emit("parentCall", ($parent) => {
          let detailRowData = $parent.$refs.detail.rowData;
          //   console.log(" $parent.", $parent.$refs.detail.rowData);

          let arr = [];
          let stringName = "";
          detailRowData.forEach((item) => {
            rowObj.forEach((j) => {
              if (j.ID == item.Produce_use) {
                stringName += stringName ? "," + j.领用单号 : j.领用单号;
                arr.push(j.ID);
              }
            });
          });
          if (stringName != "") {
            this.$message.warning("明细表已存在 " + stringName);
          }
          arr.forEach((item) => {
            let index = rowObj.findIndex((x) => x.ID == item);
            if (index != -1) {
              rowObj.splice(index, 1);
            }
          });
          let newRowObj = [];
          newRowObj = rowObj.map((item) => {
            return {
              Produce_use: item.ID,
              Goods_sub: item.产品编码,
              Goods_name: item.产品名称,
              Goods_num: item.产品数量,
              Produce_use_voucher_sub: item.领用单号,
              Fittings_num: item.配件数量,
              Total_price: item.总成本,
              shzt: item.shzt,
              Cost_price: item.成本,
              Tax_exclusive: item.无税成本,
              Tax_exclusive_total_price: item.无税总成本,
            };
          });
          newRowObj = this.base.getPushDatileWHSEName(
            newRowObj,
            "Goods_sub",
            this.WHSE_Id
          );
          $parent.$refs.detail.rowData.unshift(...newRowObj);
          $parent.WHSE_IdDisableFn();
          // this.getDatileSelectFn($parent, "Goods_sub");
        });

        this.onModelClose();
        return (this.model = false);
      } else {
        if (this.messageError) {
          this.messageError.close();
        }
        this.messageError = this.$message.error("请至少选择一条领用单");
      }
    },
    //明细表库位下拉框
    async getDatileSelectFn($parent, sub) {
      let rows = $parent.$refs.detail.rowData;
      await rows.forEach((row) => {
        $parent.$refs.detail.columns.forEach((item) => {
          if (item.field == "WHSE_name") {
            let selectUrl = `api/q_warehouse_locationList/getgoodslocation?bm=${row[sub]}`;
            item.bind.data.forEach((d) => {
              d.hidden = true;
            });
            this.http.post(selectUrl, {}, "").then((reslut) => {
              item.bind.data.forEach((d) => {
                d.hidden = true;
              });
              item.bind.data.forEach((d) => {
                reslut.rows.forEach((j) => {
                  if (d.key == j.WHSE_no) {
                    d.hidden = false;
                  }
                });
              });
              row.WHSE_name = item.bind.data.filter(
                (x) => x.hidden == false
              )[0].key;
            });
          }
        });
      });
    },
  },
};
</script>
    
    <style>
</style>